package com.sixth.service.Impl;

import com.sixth.dao.OutlibraryDao;
import com.sixth.entity.BranchAndGoods;
import com.sixth.entity.Limit;
import com.sixth.entity.OutlibraryGoods;
import com.sixth.entity.Staff;
import com.sixth.service.OutInformationService;
import com.sixth.util.CommonResult;
import com.sixth.util.LayTab;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: project01
 * @description:
 * @author: 老范
 * @create: 2021-12-20 08:48
 **/
@Service
public class OutInformationServiceImpl implements OutInformationService {
    @Autowired
    private OutlibraryDao outlibraryDao;
    @Autowired
    private HttpSession session;

    public LayTab getAllOutLibrary(Limit limit, Integer bid){
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();

        List<Map> information = outlibraryDao.getOutInformation(limit,branchid);
        Integer count = outlibraryDao.getOutInformationCount(branchid);
        LayTab layTab = new LayTab(count,information);
        return layTab;
    }

    public CommonResult ExPass(Integer branchid, Integer cid) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer i = outlibraryDao.updateOutLibrary(cid, staffid, new Date());
        if (i>0){
            List<OutlibraryGoods> outLibraryByCid = outlibraryDao.getOutLibraryByCid(cid);

            for (OutlibraryGoods goods : outLibraryByCid) {

                Integer goodsid = goods.getGoodsid();
                Integer num = goods.getNum();

                Integer countByBG = outlibraryDao.getGoodsCountByBG(branchid, goodsid);
                if (num > countByBG) {
                    return CommonResult.fail("库存已不足，请驳回检查！");
                }else {
                    outlibraryDao.batchOutLibrary(branchid, goodsid, num);
                }
            }
            return CommonResult.success(null, "操作成功！");
        }
        return CommonResult.fail();
    }

    public CommonResult ExReject(Integer cid, String msg) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer i = outlibraryDao.updateExReject(cid, staffid, new Date(), msg);
        CommonResult result;
        if (i > 0) {
            result = CommonResult.success();
        }else {
            result = CommonResult.fail();
        }
        return result;
    }
    public LayTab getAllOutLibrary1(Limit limit,Integer bid) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        List<Map> information = outlibraryDao.getOutInformation1(limit,branchid,roleid,bid);

        Integer count = outlibraryDao.getOutInformationCount1(branchid,roleid,bid);

        LayTab layTab = new LayTab(count,information);
        return layTab;
    }
    public LayTab getAllTransfer(Limit limit) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        List<Map> information = outlibraryDao.getAllTransfer(limit,branchid);
        Integer count = outlibraryDao.getAllTransferCount(branchid);

        LayTab layTab = new LayTab(count,information);
        return layTab;
    }

    //平调通过
    public CommonResult transferExPass(Integer branchid, Integer cid, Integer newbranchid) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer i = outlibraryDao.updateTransferOutLibrary(cid, staffid, new Date());
        if (i>0){
            List<OutlibraryGoods> outLibraryByCid = outlibraryDao.getOutLibraryTransferByCid(cid);

            for (OutlibraryGoods goods : outLibraryByCid) {

                Integer goodsid = goods.getGoodsid();
                Integer num = goods.getNum();

                Integer countByBG = outlibraryDao.getGoodsCountByBG(branchid, goodsid);
                BranchAndGoods branchAndGoods;

                if (num > countByBG) {
                    return CommonResult.fail("库存已不足，请驳回检查！");
                }else {
                    outlibraryDao.batchOutLibrary(branchid, goodsid, num);

                    branchAndGoods = outlibraryDao.getBranchAndGoods(goodsid, newbranchid);
                }

                if (branchAndGoods!=null){
                    outlibraryDao.transferOutLibrary(newbranchid, goodsid, num);
                }else {
                    outlibraryDao.addBranchAndGoods(goodsid, newbranchid, num);
                }

            }
            return CommonResult.success(null, "操作成功！");
        }
        return CommonResult.fail();
    }

    public CommonResult transferExReject(Integer cid, String msg) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer i = outlibraryDao.updateTransferExReject(cid, staffid, new Date(), msg);
        CommonResult result;
        if (i > 0) {
            result = CommonResult.success();
        }else {
            result = CommonResult.fail();
        }
        return result;
    }

    public LayTab getAllTransfer1(Limit limit,Integer bid) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        List<Map> information = outlibraryDao.getAllTransfer1(limit,branchid,bid,roleid);
        Integer count = outlibraryDao.getAllTransferCount1(branchid,bid,roleid);

        LayTab layTab = new LayTab(count,information);
        return layTab;
    }
}
